package com.wlb365.financing.common.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 概述：json模板工具类
 * </p>
 *
 * @author zwh
 * @version 1.0
 * @copyRight 北京微利宝信息技术有限公司  All right reserved.
 * @date 2021/5/19
 */
public class JsonSchemaUtil {

    private static ObjectMapper mapper = new ObjectMapper();

    public static void main(String[] args) {
        try{
            Map<String, Object> map = execute("{\"schema\":{\"type\":\"object\",\"required\":[\"ladOrder\",\"logOrder\",\"acceptanceNumber\",\"driName\",\"driCerNo\",\"carNo\",\"shippingWeight\",\"actWeight\",\"acceptanceImgPath\"],\"properties\":{\"ladOrder\":{\"title\":\"云商提单号\",\"type\":\"string\",\"maxLength\":64,\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"logOrder\":{\"title\":\"物流提单号\",\"type\":\"string\",\"maxLength\":64,\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"acceptanceNumber\":{\"title\":\"验收单号\",\"type\":\"string\",\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"aofRecognition\":{\"title\":\"验收人人脸识别\",\"type\":\"string\",\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"driName\":{\"title\":\"司机名称\",\"type\":\"string\",\"maxLength\":32,\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"driCerNo\":{\"title\":\"司机身份证号\",\"type\":\"string\",\"maxLength\":32,\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"carNo\":{\"title\":\"车辆车牌号\",\"type\":\"string\",\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"shippingWeight\":{\"title\":\"发货重量\",\"type\":\"string\",\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"actWeight\":{\"title\":\"实际验收重量\",\"type\":\"string\",\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"acceptanceList\":{\"title\":\"验收货物明细\",\"type\":\"array\",\"items\":{\"type\":\"object\",\"required\":[\"acceptedGoodsName\",\"acceptTmofgoods\",\"smAcceptanceWeight\"],\"properties\":{\"acceptedGoodsName\":{\"title\":\"验收货物名称\",\"type\":\"string\",\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"acceptTmofgoods\":{\"title\":\"验收货物材质\",\"type\":\"string\",\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"smAcceptanceWeight\":{\"title\":\"单一材质验收重量\",\"type\":\"string\",\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}},\"resAcceptance\":{\"title\":\"工地验收参考基价\",\"type\":\"string\",\"default\":\"0\",\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}}},\"ui:order\":[\"acceptedGoodsName\",\"acceptTmofgoods\",\"smAcceptanceWeight\",\"resAcceptance\"]},\"uniqueItems\":false},\"acceptanceImgPath\":{\"title\":\"验收图片\",\"type\":\"string\",\"ui:options\":{\"width\":\"30%\",\"placeholder\":\"请输入\"}}},\"ui:order\":[\"ladOrder\",\"logOrder\",\"acceptanceNumber\",\"aofRecognition\",\"driName\",\"driCerNo\",\"carNo\",\"shippingWeight\",\"actWeight\",\"acceptanceList\",\"acceptanceImgPath\"]},\"uiSchema\":{},\"formFooter\":{\"show\":false},\"formProps\":{\"inline\":true,\"inlineFooter\":true,\"layoutColumn\":3,\"labelWidth\":\"100px\",\"labelSuffix\":\"：\"}}");
            System.out.println(map);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
    * 将json模板文件转换为map.
    * @param json json模板文件
    * @author zwh
    * @return {key1:{title:title1},key2:[{skey1:{title:title1}},{skey2:{title:title2}}]}
    **/
    public static Map<String, Object> execute(String json) throws IOException {
        Map<String, Object> map = new HashMap<>();

        JsonNode root = mapper.readTree(json);

        JsonNode rootNode = root.get("schema").path("properties");
        fillTheElementMap(rootNode, map);

        return map;
    }

    private static void fillTheElementMap(JsonNode rootNode, Map<String, Object> map) {
        Iterator<Map.Entry<String, JsonNode>> it = rootNode.fields();
        while (it.hasNext())
        {
            Map.Entry<String, JsonNode> entry = it.next();
            JsonNode cNode = entry.getValue();
            if(cNode.path("type").toString().toLowerCase().contains("array")){
                List<Map<String, Object>> list = new ArrayList<>();
                for(JsonNode ccNode : cNode.path("items")){
                    Map<String, Object> sub = new HashMap<>();
                    fillTheElementMap(ccNode, sub);
                    if(sub.size() > 0){
                        list.add(sub);
                    }
                }
                map.put(entry.getKey(), list);
            }
            else if(cNode.path("type").toString().toLowerCase().contains("object")){
                fillTheElementMap(cNode.path("properties"), map);
            }
            else{
                if (entry.getKey() != null) {
                    map.put(entry.getKey(),  mapper.convertValue(cNode, Map.class));
                }
            }
        }
    }
}
